package com.powerManager.sdk.handlers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;

import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;

public class DepartmentHandler {

	/** -- 部门模块,部门列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String departmentJSON(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql="select * from sys_department";
		PagingResult pr = new PagingResult(sql);
		String json = null;
		try {
			json = pr.treeToJson(sql, null, "department_id", "parent_id", "0");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "text:"+json;
	}
	
	public String removeDepartment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "delete from sys_department where department_id=?";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object args = request.getParameter("ID");
		jdbc.update(sql, args);
		return "text:true";
	}
	public String updateDepartment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "update sys_department set Department_name=?,parent_id=?,remark=? where department_id=?";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object[] args = new Object[4];
		args[0] = request.getParameter("Department_name");
		args[1] = request.getParameter("DeptParentID");
		args[2] = request.getParameter("remark");
		args[3] = request.getParameter("department_id");
		jdbc.update(sql, args);
		return "text:true";
	}
	public String addDepartment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sql = "insert into sys_department(department_id,Department_name,parent_id,remark) values(GET_SEQ('SEQ_DEPARTMENT_ID','0'),?,?,?)";
		JdbcTemplate jdbc = JdbcSingleton.getJdbcTemplate();
		Object[] args = new Object[3];
		args[0] = request.getParameter("Department_name");
		args[1] = request.getParameter("parent_id");
		args[2] = request.getParameter("remark");
		jdbc.update(sql, args);
		return "text:true";
	}
}
